package com.jason.kotlin.meta.architecture2.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.jason.kotlin.meta.architecture2.api.ApiSource
import com.jason.kotlin.meta.architecture2.api.SquareService
import com.jason.kotlin.meta.architecture2.model.ArticleBean
import com.jason.kotlin.meta.architecture2.model.NavigationBean
import com.jason.kotlin.meta.architecture2.model.SystemBean
import com.jason.kotlin.meta.architecture2.model.WanDataWrapper
import com.jason.kotlin.meta.architecture2.repository.SquareApi
import com.jason.kotlin.meta.architecture2.repository.SquareApiImpl
import com.jason.library.common.base.PageStatus
import kotlinx.coroutines.launch
import java.lang.Exception

class SquareViewModel(application: Application) : AndroidViewModel(application) {

    //页面状态变化
    val pageStatusData = MutableLiveData<PageStatus>()

    val articleData = MutableLiveData<WanDataWrapper<List<ArticleBean>>>()

    val systemData = MutableLiveData<WanDataWrapper<List<SystemBean>>>()

    val navigationData = MutableLiveData<WanDataWrapper<List<NavigationBean>>>()

    private var squareApi : SquareApi

    private var curPage: Int = 0

    init {
        val squareService = ApiSource.getWanAndroidService(SquareService::class.java)
        squareApi = SquareApiImpl(squareService)

    }

    fun loadData() {
        curPage = 0
        loadData(curPage)
    }

    fun loadMoreData() {
        curPage++
        loadData(curPage)
    }

    private fun loadData(page:Int) {
        viewModelScope.launch {
            pageStatusData.postValue(PageStatus.loading())
            try {
                val articleResponse = squareApi.getSquareData(page)
                articleData.postValue(WanDataWrapper.success(articleResponse.data.datas))

                pageStatusData.postValue(PageStatus.success())
            }catch (e: Exception) {
                pageStatusData.postValue(PageStatus.error(e.toString()))
            }
        }
    }

    private fun loadSystemData() {
        viewModelScope.launch {
            pageStatusData.postValue(PageStatus.loading())
            try {
                val systemResponse = squareApi.getSystemData()
                systemData.postValue(WanDataWrapper.success(systemResponse.data))

                pageStatusData.postValue(PageStatus.success())
            }catch (e: Exception) {
                pageStatusData.postValue(PageStatus.error(e.toString()))
            }
        }
    }

    private fun loadNavigationData() {
        viewModelScope.launch {
            pageStatusData.postValue(PageStatus.loading())
            try {
                val navigationResponse = squareApi.getNavigationData()
                navigationData.postValue(WanDataWrapper.success(navigationResponse.data))

                pageStatusData.postValue(PageStatus.success())
            }catch (e: Exception) {
                pageStatusData.postValue(PageStatus.error(e.toString()))
            }
        }
    }
}